      //
//  AppDelegate.m
//  CleanerAir_Project
//
//  Created by mac on 16/4/28.
//  Copyright © 2016年 Beijing Colin El Internet. All rights reserved.
//

#import "AppDelegate.h"
#import "FatherBarController.h"
#import "drawerViewController.h"
#import "DDMenuController.h"//抽屉

#import "UMSocial.h"
#import "UMSocialQQHandler.h"
#import "UMSocialWechatHandler.h"
#import "UMSocialSinaSSOHandler.h"
#import <MAMapKit/MAMapKit.h>
#import "CoreNewFeatureVC.h"
#import "AMapLocationKit.h"

//static AppDelegate *_appDelegate = nil;
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    //设置key
    [self initKey];
    
    if ([[UIDevice currentDevice].systemVersion floatValue]>=8.0) {
        /**
         *  iOS8注册授权,设置本地通知模式
         */
        /*
         UIUserNotificationTypeNone    = 0,      不发出通知
         UIUserNotificationTypeBadge   = 1 << 0, 改变应用程序图标右上角的数字
         UIUserNotificationTypeSound   = 1 << 1, 播放音效
         UIUserNotificationTypeAlert   = 1 << 2, 是否运行显示横幅
         */
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound |UIUserNotificationTypeAlert  categories:nil];
        /**
         *  然后注册通知
         */
        [application registerUserNotificationSettings:settings];
    }
   // 设置window
    
    [[UITabBar appearance] setShadowImage:[[UIImage alloc]init]];
    
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    [NSThread sleepForTimeInterval:3.0];//设置启动页面时间
    
    UIWindow *window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    //    UIWindow *window = [[UIWindow alloc] initWithFrame:self.window.bounds];
    //         NSLog(@"%f",[UIScreen mainScreen].bounds.size.height);
    self.window = window;
    
    //判断是否需要显示：（内部已经考虑版本及本地版本缓存）
    BOOL canShow = [LoginSingleton sharedLoginSingleton].isFirstStart;
    
    //测试代码，正式版本应该删除
    //    canShow = YES;
    
    if(canShow){
        
        NewFeatureModel *m1 = [NewFeatureModel model:[UIImage imageNamed:@"w-1"]];
        NewFeatureModel *m2 = [NewFeatureModel model:[UIImage imageNamed:@"w-2"]];
        NewFeatureModel *m3 = [NewFeatureModel model:[UIImage imageNamed:@"w-3"]];
        NewFeatureModel *m4 = [NewFeatureModel model:[UIImage imageNamed:@"w-4"]];
        
        window.rootViewController = [CoreNewFeatureVC newFeatureVCWithModels:@[m1,m2,m3,m4] enterBlock:^{
            
            [self table2];
        }];
        
    }else{
        
        [self table2];
    }
   
    
//    NSLog(@"%@",APIKey);

    
//    [UMSocialData setAppKey:@"507fcab25270157b37000010"];
//    
//    [UMSocialQQHandler setQQWithAppId:@"100424468" appKey:@"c7394704798a158208a74ab60104f0ba" url:@"http://www.umeng.com/social"];
//    
//    [UMSocialWechatHandler setWXAppId:@"wxd930ea5d5a258f4f" appSecret:@"db426a9829e4b49a0dcac7b4162da6b6" url:@"http://www.umeng.com/social"];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

#pragma mark -- 设置各种key
- (void)initKey{
    
//    高德
    [AMapSearchServices sharedServices].apiKey = gaodeAppKey;
    //    //配置用户Key
    [MAMapServices sharedServices].apiKey = gaodeAppKey;
    [AMapLocationServices sharedServices].apiKey = gaodeAppKey;
    
//    友盟
    
    [UMSocialData setAppKey:@"574be4c4e0f55ab427000fb6"];
    
    [UMSocialQQHandler setQQWithAppId:@"1105501218" appKey:@"nQKjd18fmo3n2wG6" url:@"http://www.umeng.com/social"];
    
    
    [UMSocialWechatHandler setWXAppId:@"wx1a7c631573632540" appSecret:@"3020d14e16870ab65669c94c172dce14" url:@"http://www.umeng.com/social"];
    
  
    
//    [UMSocialSinaSSOHandler openNewSinaSSOWithAppKey:@"265523627"
//                                              secret:@"51774579da5508326de61ee5b27093af"
//                                         RedirectURL:@"http://sns.whalecloud.com/sina2/callback"];
    [UMSocialSinaSSOHandler openNewSinaSSOWithAppKey:@"265523627" RedirectURL:@"http://www.cleanerair.com.cn/"];
    
    
    //    初始化登录单例
    [LoginSingleton sharedLoginSingleton];
    
}

-(BOOL)application:(UIApplication*)app openURL:(NSURL*)url
           options:(NSDictionary<NSString *,id>*)options{
    
    return [UMSocialSnsService
           handleOpenURL:url]; 
}

//- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
//{
//    return  [UMSocialSnsService handleOpenURL:url];
//}


-(void)table2{
    _sideViewController = [[YRSideViewController alloc]initWithNibName:nil bundle:nil];
    _sideViewController.needSwipeShowMenu =NO;
    
    drawerViewController *team = [[drawerViewController alloc]init];
    
    FatherBarController *main = [[FatherBarController alloc]init];
    
    // 设置主视图视图控制器
    _sideViewController.rootViewController = main;
    // 设置左侧视图控制器
    _sideViewController.leftViewController = team;
    _sideViewController.leftViewShowWidth = (MAIN_WIDTH)-((MAIN_WIDTH)*0.29);
    // 默认开启的可滑动展示
    _sideViewController.needSwipeShowMenu = NO;
    [_sideViewController setNeedSwipeShowMenu:NO];
    //[[AppDelegate shareInstance]getSideViewController].needSwipeShowMenu=NO;
    self.window.rootViewController = _sideViewController;
    
    [_sideViewController setRootViewMoveBlock:^(UIView *rootView, CGRect orginFrame, CGFloat xoffset) {
        //使用简单的平移动画
        rootView.frame=CGRectMake(xoffset, orginFrame.origin.y, orginFrame.size.width, orginFrame.size.height);
    }];
}


//+ (AppDelegate *)shareInstance
//{
//    if (_appDelegate == nil)
//    {
//        _appDelegate = [[AppDelegate alloc] init];
//    }
//    
//    return _appDelegate;
//
//}

#pragma makr - 实现跳转菜单栏方法
- (YRSideViewController *)getSideViewController
{
    return _sideViewController;
}

//禁止横屏
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    return UIInterfaceOrientationMaskPortrait;
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    
    //当App进去前台的时候
    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

// 本地通知回调函数，当应用程序在前台时调用
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
    
    // 这里真实需要处理交互的地方
    // 获取通知所带的数据
    NSString *notMess = [notification.userInfo objectForKey:@"key"];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"本地通知"
                                                    message:notMess
                                                   delegate:nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];
    
    // 更新显示的徽章个数
    NSInteger badge = [UIApplication sharedApplication].applicationIconBadgeNumber;
    badge--;
    badge = badge >= 0 ? badge : 0;
    [UIApplication sharedApplication].applicationIconBadgeNumber = badge;
    
    // 在不需要再推送时，可以取消推送
    [LoginSingleton cancelLocalNotificationWithKey:@"key"];
}

@end
